package com.monster.dao;

import com.monster.pojo.Comment;
import com.monster.pojo.User;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class CommentDaoImpl extends BaseDao implements CommentDao {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");

    /**
     * 通过type_id和comment_type查询comment表获得评论信息列表，其中comment_type分别为：
     * 1歌曲，2歌单，3专辑
     * @param id
     * @param type
     * @return Comment集合
     */
    @Override
    public ArrayList<Comment> listCommentsByTypeId(Long id, int type) {
        ArrayList<Comment> comments = new ArrayList<>();
        String sql = "SELECT * FROM comment WHERE comment_type=? AND type_id=? ORDER BY comment_date DESC";
        Object[] obj = {type, id};
        rs = executeQuery(sql, obj);
        try {
            while (rs.next()) {
                Comment comment = new Comment();
                comment.setId(rs.getLong(1));//评论ID
                comment.getUser().setId(rs.getLong(2));//评论用户ID
                comment.setContent(rs.getString(5));//评论内容
                String date = sdf.format(rs.getDate(6));
                if ("2019".equals(date.substring(0, 4))) {
                    date = date.substring(5);
                }
                comment.setDate(date);//评论时间
                comment.setZan(rs.getInt(7));//评论点赞数
                comment.setReplyId(rs.getLong(8));
                comments.add(comment);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return comments;
    }

    @Override
    public int addComment(Comment comment) {
        String sql = "INSERT INTO comment VALUES(default,?,?,?,?,default,0)";
        Object[] obj = {comment.getUser().getId(), comment.getCommentType(), comment.getTypeId(),
            comment.getContent()};
        return executeUpdate(sql, obj);
    }

    @Override
    public int deleteComment(Long id) {
        String sql = "DELETE FROM comment WHERE comment_id=?";
        Object[] obj = {id};
        return executeUpdate(sql, obj);
    }

    /**
     * 根据评论id查询评论信息
     * @param id
     * @return
     */
    @Override
    public Comment getCommentByCommentId(Long id) {
        Comment comment = null;
        String sql = "SELECT * FROM comment WHERE comment_id=?";
        Object[] obj = {id};
        rs = executeQuery(sql, obj);
        try {
            while (rs.next()) {
                comment = new Comment();
                comment.setId(id);//评论ID
                comment.getUser().setId(rs.getLong(2));//评论用户ID
                comment.setContent(rs.getString(5));//评论内容
                String date = sdf.format(rs.getDate(6));
                if ("2019".equals(date.substring(0, 4))) {
                    date = date.substring(4);
                }
                comment.setDate(date);//评论时间
                comment.setZan(rs.getInt(7));//评论点赞数
                comment.setReplyId(rs.getLong(8));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return comment;
    }


}
